1 module game_selector;
2 import commons;
3 
4 bool isGameFolderValid(string gameFolder)
5 {
6     return std.file.exists(buildNormalizedPath(gameFolder, "assets")) &&
7             std.file.exists(buildNormalizedPath(gameFolder, "source")) && 
8             std.file.exists(buildNormalizedPath(gameFolder, "dub.template.json"));
9 }
10 
11 
12 private __gshared bool hasTypedGamepath = false;
13 private ChoiceResult typeGamePath(Choice* self, ref Terminal t, ref RealTimeConsoleInput input, in CompilationOptions _)
14 {
15     string gamePath;
16     while(!gamePath.length)
17     {
18         gamePath = getValidPath(t, "Type your game path: ");
19         if(!isGameFolderValid(gamePath))
20         {
21             t.writelnError("The selected path '" ~ gamePath ~ "' is not valid. Please select a valid game folder.");
22             gamePath = null;
23         }
24     }
25 
26     t.writelnSuccess("Selected game path '", gamePath, "'");
27     changeGamePath(t, gamePath);
28     hasTypedGamepath = true;
29 
30     return ChoiceResult.Continue;
31 }
32 
33 void changeGamePath(ref Terminal t, string newGamePath)
34 {
35     configs["gamePath"] = newGamePath;
36     t.writelnHighlighted("Opening project at ", newGamePath);
37     openSourceCodeEditor(newGamePath);
38     clearCache();
39 }
40 
41 ChoiceResult selectGameFolder(Choice* c, ref Terminal t, ref RealTimeConsoleInput input, in CompilationOptions cOpts)
42 {
43     import std.array;
44     import std.algorithm;
45     Choice[] extraChoices = 
46     [
47         Choice("Type the game path manually", &typeGamePath),
48         getBackChoice()
49     ];
50     Choice[] choices = getProjectsAvailable().map!((string name) => Choice(name, null)).array;
51     if(isGameFolderValid(std.file.getcwd()))
52         choices = Choice(std.file.getcwd(), null) ~ choices;
53     Choice* selectedChoice = selectInFolderExtra(
54         "Select your game",
55         getHipPath("projects"), t, input, choices, extraChoices
56     );
57     if(selectedChoice.onSelected != null)
58         selectedChoice.onSelected(selectedChoice, t, input, cOpts);
59     // Chose a path
60     if(selectedChoice.onSelected == null)
61         changeGamePath(t, selectedChoice.name);
62     if(hasTypedGamepath || selectedChoice.onSelected == null)
63     {
64         configs["selectedChoice"] = 0;
65         engineConfig["defaultProject"] = configs["gamePath"].str;
66         updateEngineFile();
67         updateConfigFile();
68     }
69 
70     return ChoiceResult.Back;
71 }